
require([ 'config' ], function(config) {

	require([ 'util', 'jquery', 'Spinner', 'Cart' ], function(util, $, Spinner,
			Cart) {
		var isbalance= 1;
		$('#back').click(function() {

			var url = "?/Index/index";
			window.location.href = url;
		});

		var orderId;
		var isPaying = false; 
		$('#update-address').click(function() {
			var addressId = $('#addressId').val();
			var url = shoproot + "?/UserAddress/list_address";

			if(addressId == ''){
				
				url =  shoproot + "?/UserAddress/edit_address";
			}
			window.location.href = url;

		});

		function payOrder() {
			if(isPaying){
				return;
			}
			if(Cart.count() == 0)
				return;
			var addressId = $('#addressId').val();

			if (addressId == '') {
				alert('请选择要派送的地址!');
				return;
			}

			var reciTex = '';
			var rcHead = '';
			var time = $('#time').val();
			var couponId = $('#orderCouponId').val()+","+$('#userCouponId').val();
			
			var url = shoproot + "?/Order/createOrder";
			isPaying = true;
			$.post(url, {
				cartData : window.localStorage.getItem('cart'),
				addrData : addressId,
				reciHead : rcHead,
				reciTex : reciTex,
				time : time,
				coupon : couponId,
				isbalance:	isbalance

			}, orderGenhandleReq);
		}

		function orderGenhandleReq(id) {
			if(id.ret_code < 0){
				alert(id.ret_msg);
				return;
			}
			
			
			// alert(JSON.stringify(id));
			orderId = parseInt(id.ret_msg);
			Cart.clear();
			
			if (orderId > 0) {
				$.post(shoproot + "?/Order/ajaxOrderPay/", {
					orderId : orderId
				}, function(bizPackage) {
					isPaying = false;
					if (bizPackage == -1) {

						alert('无法支付，请联系客服人员!');

					} else {
						if (bizPackage == 1) {
							// 全额支付
					        history.replaceState(null, "CheersLife 下午茶", "?/Index/index");
							location.href = '?/Uc/orderlist/';
						} else {
							// alert(JSON.stringify(bizPackage));
							// 订单映射
							bizPackage.success = wepayCallback;
							bizPackage.cancel = wepayCancelCallback;
							// 发起微信支付
							wx.chooseWXPay(bizPackage);

						}
					}

				});

			} else {
				alert('订单无效或者已过期')
				isPaying = false;
			}
		}
		/**
		 * 微信支付回调
		 * 
		 * @param {type}
		 *            res
		 * @returns {undefined}
		 */
		function wepayCallback(res) {
	        history.replaceState(null, "CheersLife 下午茶", "?/Index/index");
			window.payed = true;
			window.location.href = shoproot
					+ '?/Order/expressDetail/?order_id=' + orderId;
			$('#wechat-payment-btn').removeClass('disable').html('微信安全支付');
		}

		function wepayCancelCallback(res) {
	        history.replaceState(null, "CheersLife 下午茶", "?/Index/index");
			window.location.href = shoproot
					+ '?/Order/expressDetail/?order_id=' + orderId;

		}

		$('#pay').click(payOrder);
	
		function balance_change() {
			$(".yes-no").click(
					function() {
						var yes = $(".yes-no img").attr("src").indexOf("yes");
						if (yes > 0) {
							var replace_yes = $(".yes-no img").attr("src")
									.replace('yes', 'no');
							$(".yes-no img").attr("src", replace_yes);
							isbalance = 0;
						} else if (yes <= 0) {
							var replace_no = $(".yes-no img").attr("src")
									.replace('no', 'yes');
							$(".yes-no img").attr("src", replace_no);
							
							isbalance = 1;
						}
					})
		}
		
		function order_title_wd(){
			var web_win_wd=$(window).width();
			order_name_wd=web_win_wd-103;
			$(".pro-name").css({'max-width':order_name_wd});
			}
         function express_title_wd(){
			var web_win_wd=$(window).width();
			order_name_wd=web_win_wd-103;
			$(".order-pro-list p .title-pro").css({'max-width':order_name_wd});
			}

            var time_data;
		$(document).ready(function() {

			balance_change();
			order_title_wd();
			express_title_wd();
                          $.ajax({
                                 dataType: 'json',
                                 cache: true,
                                 url: 'deliverTime.php',
                                 type: 'GET',
                                 success: function(result) {
                                 time_data = result.data;
                                 time_str = time_data[0].name+time_data[0].child[0].name+' '+time_data[0].child[0].child[0].name;
                                 $('#time').val(time_str);
                                 },
                                 accepts: {
                                 json: "application/json, text/javascript, */*; q=0.01"
                                 }
                                 });
			 
		var selectArea = new MobileSelectArea();
        selectArea.init({trigger:'#time',value:$('#hd_time').val(),data:'deliverTime.php'});
		});


	});

});

